﻿using Simhopp.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Data;


namespace Simhopp.Client
{
    public partial class Presenter : IPresenterBindings, INotifyPropertyChanged
    {
        #region FIELD REGION

        private String scoreVerification = "";
        private String userInfo = "";
        private NowJumpingDataStruct nowJumpingData;

        #endregion
        #region PropertyUpdates
        //EventHandler for registering property-observers
        public event PropertyChangedEventHandler PropertyChanged;



        #region PROPERTIES  
        public String ScoreVerification 
        {
            get { return scoreVerification; }
            set
            {
                scoreVerification = value;
                NotifyPropertyChanged();
            }
        }
        public ReadOnlyCollection<DivingType> DivingTypes
        {
            get 
            {
                return Competition.DivingTypeList;
            }
        }

        public ICollectionView DivingTypesOne
        {
            get
            {
                return filterDivingTypes(new Collection<DivingType>(Competition.DivingTypeList), 0);
            }
        }
        public ICollectionView DivingTypesTwo
        {
            get
            {
                return filterDivingTypes(new Collection<DivingType>(Competition.DivingTypeList), 1);
            }
        }
        public ICollectionView DivingTypesThree
        {
            get
            {
                return filterDivingTypes(new Collection<DivingType>(Competition.DivingTypeList), 2);
            }
        }
        public ReadOnlyObservableCollection<User> Judges
        {
            get
            {
                return Competition.Judges;
            }
        }
        public ListCollectionView JudgesOnly
        {
            get
            {
                return Competition.JudgesOnly;
            }
        }
        public ListCollectionView AccessLevelJudge
        {
            get
            {
                return filterJudge((Competition.Judges), AccountTypes.Judge);
            }
        }
        public ListCollectionView AccessLevelHeadJudge
        {
            get
            {
                return filterJudge((Competition.Judges), AccountTypes.HeadJudge);
            }
        }
        public Dictionary<int, String> Competitions
        {
           

            get
            {
                return _competitions.Dic;
            }

        }

        public ReadOnlyObservableCollection<Diver> DiverList
        {
            get
            {
                return Competition.Participants;
            }
        }
        public NowJumpingDataStruct NowJumpingData
        {
            get
            {
                return nowJumpingData;
            }
            set
            {
                nowJumpingData = value;
                NotifyPropertyChanged();
            }
        }

        public Competition Competition
        {
            get { return _competition; }
            private set
            {
                
                _competition = value;
                NotifyPropertyChanged();
            }
        }

        #endregion
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        #region DataManiupulationProperties
        protected ICollectionView filterDivingTypes(Collection<DivingType> divingTypes, int dive)
        {
            ICollectionView diveFilter = CollectionViewSource.GetDefaultView(divingTypes);
            diveFilter.Filter = delegate(object obj)
            {
                var _diveType = obj as DivingType;
                if (_diveType.Height == Competition.Heights[dive])
                {
                    return true;
                }
                return false;
            };
            return diveFilter;
        }
        
        protected ListCollectionView filterJudge(ReadOnlyObservableCollection<User> judge, AccountTypes access)
        {
            ListCollectionView judgeFilter = new ListCollectionView(judge);
            judgeFilter.Filter = delegate(object obj)
            {
                var _judge = obj as User;
                if (_judge.AccountType == access)
                {
                    return true;
                }
                return false;
            };
            return judgeFilter;
        }

        public String HeadJudgeViewVisibilityState
        {
            get
            {
                return ActiveJudge.AccountType == AccountTypes.HeadJudge ? "Visible" : "Collapsed";
            }
        }

        public String JudgeViewVisibilityState
        {
            get
            {
               if (ActiveJudge.AccountType == AccountTypes.Judge)
                {
                    return "Visible";
                }
                else if (ActiveJudge.AccountType == AccountTypes.HeadJudge)
                {
                    return "Visible";
                }
                else
                {
                    return "Collapsed";
                }
            }
        }

        public String UserInfo
        {
            get
            { 
                return userInfo; 
            }
            set 
            {  
                userInfo = value;
                NotifyPropertyChanged();
            }
        }

        public String AdministratorViewVisibilityState
        {
            get
            {
                return ActiveJudge.AccountType == AccountTypes.Administrator ? "Visible" : "Hidden";
            }
        }

      
        #endregion
    }
}
